<?php

namespace app\admin\validate\user;

use think\Validate;
error_reporting(E_ERROR);
class Appbusiness extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'velocity' => 'require|checkVelocity:thinkphp',
        'diagnosis' => 'require|checkDiagnosis:thinkphp',
        'other' => 'require|checkOther:thinkphp',
        'user_id' => 'require|unique:user_app_business',
    ];
    /**
     * 提示消息
     */
    protected $message = [
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
    ];
    public function __construct(array $rules = [], $message = [], $field = [])
    {
        $this->field = [
            'user_id' => __('User_id')
        ];
        parent::__construct($rules, $message, $field);
    }
    /***********************************
    [
        {
            "name":"节点1",
            "url":"192.168.199.199:8080"
        },
        {
            "name":"节点2",
            "url":"192.168.199.199:8080"
        },
        {
            "name":"节点3",
            "url":"192.168.199.199:8080"
        }
    ]
    ***********************************/
    protected function checkVelocity($value,$rule,$data=[]){
        $is_json = is_json($value);
        if(is_json($value)){
            $array_value = json_decode($value,true);
            if(empty($array_value)){
                return "网速测速：不能是空json";
            }
            // dump($array_value);
            // if (count($array_value) == count($array_value, 1)) {
            //     echo '是一维数组';
            // } else {
            //     echo '不是一维数组';
            // }
            // die;
            
            foreach($array_value as $key => $value){
                if(!(is_array($value) && count($value)>0)){
                    return '网速测速：输入json格式不正确，形如[{"name":"xx","url":"xx"},{"name":"xx","url":"xx"}]';
                }
                if(!isset($value['name'])){
                    return "网速测速：json中name属性不存在";
                }
                if(empty($value['name'])){
                    return "网速测速：json中name属性值不能为空";
                }
                if(!isset($value['url'])){
                    return "网速测速：json中url属性不存在";
                }
                if(empty($value['name'])){
                    return "网速测速：json中name属性值不能为空";
                }
            }
            // die;
            return true;
        }else{
            return "网速测速：请输入合法的json数据";
        }
    }
    /*******************************
    {
        "operator":"111.48.26.136",
        "internet":"www.baidu.com"
    }
    *******************************/
    protected function checkDiagnosis($value,$rule,$data=[]){
        $is_json = is_json($value);
        if(is_json($value)){
            $array_value = json_decode($value,true);
            if(!isset($array_value['operator'])){
                return "诊断测试：json中operator属性不存在";
            }
            if(empty($array_value['operator'])){
                return "诊断测试：json中operator属性值不能为空";
            }
            if(!isset($array_value['internet'])){
                return "诊断测试：json中internet属性不存在";
            }
            if(empty($array_value['internet'])){
                return "诊断测试：json中internet属性值不能为空";
            }
            return true;
        }else{
            return "诊断测试：请输入合法的json数据";
        }
    }

    /*******************************
    {
                "telecomService": "103.78.125.193",
                "unicomService": "220.194.79.73",
                "moveService": "39.156.66.18",
                "localSpeedTest": {
                    "name": "内网",
                    "content": "内网测速",
                    "files": [
                        "http://106.2.230.2:3000/random3000x3000.jpg",
                        "http://106.2.230.2:3000/random4000x4000.jpg"
                    ]
                }
    }
    *******************************/

    public function checkOther($value,$rule,$data=[])
    {
        $is_json = is_json($value);
        if(is_json($value)){
            $array_value = json_decode($value,true);
            if(empty($array_value['telecomService'])){
                return "其他测试：json中telecomService属性不能为空";
            }
            if(empty($array_value['unicomService'])){
                return "其他测试：json中unicomService属性值不能为空";
            }
            if(empty($array_value['moveService'])){
                return "其他测试：json中moveService属性不能为空";
            }
            if(!isset($array_value['localSpeedTest'])){
                return "其他测试：json中localSpeedTest属性值不存在";
            }
            if(!isset($array_value['localSpeedTest']['name'])){
                return "其他测试：json中localSpeedTest属性中name属性不存在";
            }
            if(!isset($array_value['localSpeedTest']['content'])){
                return "其他测试：json中localSpeedTest属性中content属性不存在";
            }
            if(!isset($array_value['localSpeedTest']['files'])){
                return "其他测试：json中localSpeedTest属性中files属性不存在";
            }
            return true;
        }else{
            return "其他测试：请输入合法的json数据";
        }
    }



}
